package com.aishua.aishuawallet.engin;

import com.aishua.aishuawallet.bean.IRequestString;
import com.aishua.aishuawallet.utils.LogUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class SocketManager {

	private ExecutorService mThreadPool = null;
	private static SocketManager manager = new SocketManager();
	private SocketManager(){
		if (null == mThreadPool) {
			mThreadPool = Executors.newFixedThreadPool(1);
		}
	}
	
	public static SocketManager getManager(){
		return manager;
	}
	/**
	 * ==============================判断是否绑定过==============================
	 */
	public void request(String request, IResponseListener listener) {
		LogUtils.v("MianManager===request");
		if(request!=null){
//			if(socket!=null&&!socket.isClosed()){
//				try {
//					socket.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}				
//			}
//			if(socket==null){
//				socket = new Socket();
//				try {
//					socket.setKeepAlive(false);
//				} catch (SocketException e) {
//					e.printStackTrace();
//				}
//			}
			
			Task task = new Task(request);
			task.attachTask(task);
			task.setResponseListener(listener);
//			mThreadPool.submit(task);
			executeTask(task);
			
		}
	}
	
	public void executeTask(Runnable runnable){
		mThreadPool.submit(runnable);
	}
//
//	/**
//	 * ==============================绑定账号==============================
//	 */
//	public void socketBind(Handler handler, String phone, String idCard, String name) {
//		LogUtils.v("SOCKET_BIND-->");
//		mHandler = handler;
//		data.setCard_number(XTApplication.appSequence);
//		data.setPhone(phone);
//		data.setIdCard(idCard);
//		data.setName(name);
//		socketReq = data.getCmdBind();
//		LogUtils.v("socketReq-->" + socketReq);
//
//		new Thread() {
//			public void run() {
//				try {
//					socket = new Socket(Constant.SOCKET_IP, Constant.SOCKET_PORT);
//					OutputStream os = socket.getOutputStream();
//					OutputStreamWriter osw = new OutputStreamWriter(os);
//					BufferedWriter bw = new BufferedWriter(osw);
//
//					bw.write(socketReq.trim() + "\r\n");
//					bw.flush();
//
//					InputStream is = socket.getInputStream();
//					InputStreamReader isr = new InputStreamReader(is, "utf-8");
//					BufferedReader br = new BufferedReader(isr);
//					socketRes = br.readLine();
//					LogUtils.v("SOCKET-SERVER-RES->" + socketRes);
//					if ("1702".equals(socketRes)) {
//						mHandler.sendEmptyMessage(Constant.BIND_RES_CODE_FAIL_MD5);
//					}
//					if ("1703".equals(socketRes)) {
//						mHandler.sendEmptyMessage(Constant.BIND_RES_CODE_FAIL_SERVER);
//					}
//					if ("1700".equals(socketRes)) {
//						mHandler.sendEmptyMessage(Constant.BIND_RES_CODE_SUCCESS);
//					}
//
//				} catch (UnknownHostException e) {
//					e.printStackTrace();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			};
//		}.start();
//	}

}
